import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/login.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userAccount = req.getParameter("user"); // 对应表单中的 user_account字段
        String password = req.getParameter("password");// 对应表单中的 password字段
        UserDAO userDAO = new UserDAO();
        User user = userDAO.findByUserAccount(userAccount);
        if (user != null && user.getPassword().equals(password)) {

            resp.sendRedirect(req.getContextPath() + "/index.jsp"); // 跳转到商店主页面
        } else {
            req.setAttribute("msg", "用户账号或密码错误");
            req.getRequestDispatcher("login.jsp").forward(req, resp);

        }

    }
}

